﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using QuanLyThuVien.DTO;
using System.Data;
using QuanLyThuVien.DAL;
using QuanLyThuVien.Supporter;
using System.Data.SqlClient;

namespace QuanLyThuVien.DAL
{
    //----------------------------------------------------------------------
    //Desc: class kết nối với bảng TK_TraSachTre
    //----------------------------------------------------------------------
    public class DALTK_TraSachTre
    {
        //----------------------------------------------------------------------
        //Desc: constructor mặc định
        //----------------------------------------------------------------------
        public DALTK_TraSachTre()
        { }

        //----------------------------------------------------------------------
        //Desc: chuyển dòng dữ liệu thành TK_TRASACHTRE
        //----------------------------------------------------------------------
        public DTOTK_TraSachTre ConvertToTKTraSachTre(DataRow row)
        {
            DTOTK_TraSachTre tktrasachtre = new DTOTK_TraSachTre();
            try
            {
                tktrasachtre.NgayTK = DateTime.Parse(row["NgayTK"].ToString());
            }
            catch { }
            try
            {
                tktrasachtre.MaCuonSach = row["MaCuonSach"].ToString();
            }
            catch { }
            try
            {
                tktrasachtre.NgayMuon = DateTime.Parse(row["NgayMuon"].ToString());
            }
            catch { }
            try
            {
                tktrasachtre.SoNgayTraTre = Int32.Parse(row["SoNgayTraTre"].ToString());
            }
            catch { }

            return tktrasachtre;
        }

        //----------------------------------------------------------------------
        //Desc: đếm phiếu thống kê
        //----------------------------------------------------------------------
        public int DemPhieuTK(DateTime ngaytk)
        {
            if (DatabaseManager.DbConnection == null)
                return -1;
            string spName = "spTK_TraSachTre_DemPhieuTK";
            SqlParameter sqlprNgayTK = new SqlParameter("@NgayTK", SqlDbType.Date) { Value = ngaytk };
            object obj = DatabaseManager.DbConnection.ExecuteStoredProcedureScalar(spName, sqlprNgayTK);
            if (obj == null)
                return -1;
            int dem;
            try
            {
                dem = (int)obj;
            }
            catch
            {
                return -1;
            }

            return dem;
        }

        //----------------------------------------------------------------------
        //Desc: cập nhật phiếu thống kê
        //----------------------------------------------------------------------
        public bool CapNhat(DTOTK_TraSachTre tktrasachtre)
        {
            if (DatabaseManager.DbConnection == null)
                return false;
            string spName = "spTK_TraSachTre_CapNhatPhieuTK";
            SqlParameter sqlprThaoTac = new SqlParameter("@ThaoTac", SqlDbType.Int) { Value = 1 };
            SqlParameter sqlprNgayTK = new SqlParameter("@NgayTK", SqlDbType.Date) { Value = tktrasachtre.NgayTK };
            SqlParameter sqlprMaCuonSach = new SqlParameter("@MaCuonSach", SqlDbType.VarChar, 15)
            { Value = tktrasachtre.MaCuonSach };
            SqlParameter sqlprNgayMuon = new SqlParameter("@NgayMuon", SqlDbType.Date) { Value = tktrasachtre.NgayMuon };
            SqlParameter sqlprSoNgayTraTre = new SqlParameter("@SoNgayTraTre", SqlDbType.Int) { Value = tktrasachtre.SoNgayTraTre };
            return DatabaseManager.DbConnection.ExecuteStoredProcedureNonQuery(spName, sqlprThaoTac, sqlprNgayTK, sqlprMaCuonSach, sqlprNgayMuon, sqlprSoNgayTraTre);
        }

        //----------------------------------------------------------------------
        //Desc: lấy dữ liệu thống kê
        //----------------------------------------------------------------------
        public List<DTOTK_TraSachTre> LayDuLieuThongKe(DateTime ngaytk)
        {
            if (DatabaseManager.DbConnection == null)
                return null;
            string spName = "spTK_TraSachTre_LayPhieuTK";
            SqlParameter sqlprNgayTK = new SqlParameter("@NgayTK", SqlDbType.Date) { Value = ngaytk };
            DataTable data = DatabaseManager.DbConnection.ExecuteStoredProcedure(spName, sqlprNgayTK);
            if (data == null)
                return null;
            List<DTOTK_TraSachTre> list = new List<DTOTK_TraSachTre>();
            foreach (DataRow row in data.Rows)
                list.Add(ConvertToTKTraSachTre(row));

            return list;
        }
    }
}
